<template>
  <abstract-dialog
    :loading="loading"
    append-to-body
    :title="title"
    :value="value"
    @close="cancel"
    @open="open"
  >
    <abstract-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input
          v-model="form.oldPassword"
          placeholder="请输入旧密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input v-model="form.password" placeholder="请输入新密码"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="aginPassword">
        <el-input
          v-model="form.aginPassword"
          placeholder="请输入确认密码"
        ></el-input>
      </el-form-item>
    </abstract-form>
    <template v-slot:footer>
      <el-button plain size="small" @click="closeDialog">取 消</el-button>
      <el-button size="small" type="primary" @click="confirm">确 定</el-button>
    </template>
  </abstract-dialog>
</template>

<script>
import AbstractDialog from '@/components/abstract/Dialog'
import dialogMixin from '@/mixin/dialogMixin'
import AbstractForm from '@/components/abstract/Form'
import { elSuccess } from '@/utils/message'
import { request } from '@/api/request'

export default {
  name: 'EditPasswordDialog',

  components: {
    AbstractDialog,
    AbstractForm
  },

  mixins: [dialogMixin],

  props: {
    value: Boolean,
    data: { type: Object, default: () => ({}) }
  },

  data() {
    let validateAginPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      users_id: '',
      loading: false,
      crowdTags: [],
      title: '修改密码',
      form: {
        oldPassword: null,
        password: null,
        aginPassword: null
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        aginPassword: [
          { required: true, message: '请输入确认新密码', trigger: 'blur' },
          { validator: validateAginPassword, trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    open() {},

    clearForm() {
      this.form.oldPassword = null
      this.form.password = null
      this.form.aginPassword = null
      this.$nextTick(() => this.$refs.form.clearValidate())
    },

    cancel() {
      this.closeDialog()
      this.clearForm()
    },

    confirm() {
      if (this.loading) return
      this.$refs.form.validate((v) => {
        if (!v) return
        this.loading = true
        const promise = request(
          'company/users/allUsers/addSubmit',
          {
            ...this.form,
            crowdMarkerIds: JSON.stringify(this.form.crowdMarkerIds)
          },
          () => {},
          { isDelNull: true }
        )
        promise
          .then(() => {
            elSuccess()
            this.$emit('success')
            this.cancel()
          })
          .finally(() => this.$nextTick(() => (this.loading = false)))
      })
    }
  }
}
</script>
